package com.master.demo.im.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.master.common.api.IPageInfo;
import com.master.common.api.Query;
import com.master.common.api.ResponseInfo;
import com.master.common.exception.ErrorException;
import com.master.common.utils.DateUtil;
import com.master.common.utils.DozerUtil;
import com.master.common.validator.ValidatorUtil;
import com.master.demo.im.mapper.SessionMessageMapper;
import com.master.demo.im.pojo.command.SessionMessageCommand;
import com.master.demo.im.pojo.entity.SessionMessage;
import com.master.demo.im.pojo.vo.SessionMessageVO;
import com.master.demo.im.service.ISessionListService;
import com.master.demo.im.service.ISessionMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会话内容 服务实现类
 *
 * @author: Yang
 * @date: 2022-08-09
 * @version: 1.0.0
 * Copyright Ⓒ 2021 恒翔 Computer Corporation Limited All rights reserved.
 */
@Service
public class SessionMessageServiceImpl extends ServiceImpl<SessionMessageMapper, SessionMessage> implements ISessionMessageService {
    @Autowired
    private SessionMessageMapper sessionMessageMapper;
    @Autowired
    private ISessionListService sessionListService;

    /**
     * 根据id获取会话内容信息
     *
     * @param id 会话内容id
     * @return
     */
    private SessionMessage getSessionMessage(Long id) {
        SessionMessage sessionMessage = sessionMessageMapper.selectById(id);
        if (ValidatorUtil.isNull(sessionMessage)) {
            throw new ErrorException("会话内容信息不存在，无法操作");
        }
        return sessionMessage;
    }

    @Override
    public ResponseInfo<IPageInfo<SessionMessageVO>> pageSessionMessage(Query query) {
        IPage<SessionMessageVO> iPage = sessionMessageMapper.pageSessionMessage(new Page<>(query.getCurrent(), query.getSize()), query);
        return ResponseInfo.success(new IPageInfo(iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getRecords()));
    }

    @Override
    public ResponseInfo<List<SessionMessageVO>> listSessionMessage(Query query) {
        return ResponseInfo.success(sessionMessageMapper.listSessionMessage(query));
    }

    @Override
    public ResponseInfo<SessionMessageVO> getSessionMessageById(Long id) {
        return ResponseInfo.success(DozerUtil.convertor(getSessionMessage(id), SessionMessageVO.class));
    }

    /**
     * 插入会话内容，同时插入会话列表
     *
     * @param command 会话内容
     * @return
     */
    @Override
    public ResponseInfo saveSessionMessage(SessionMessageCommand command) {
        SessionMessage message = DozerUtil.convertor(command, SessionMessage.class);
        message.setCreateUser(command.getOperater());
        message.setTimestamp(DateUtil.getTimestamp());
        sessionMessageMapper.insert(message);
        return ResponseInfo.success();
    }

    @Override
    public ResponseInfo removeSessionMessageByIds(List<Long> ids) {
        sessionMessageMapper.deleteBatchIds(ids);
        return ResponseInfo.success();
    }

}
